package com.gandong8.trace.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gandong8.trace.biz.service.UserService;
import com.gandong8.trace.dao.domain.generated.User;
import com.gandong8.trace.dao.service.generated.PlusUserService;
import com.gandong8.trace.domain.common.PageData;
import com.gandong8.trace.domain.common.QueryData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    //global.druidOpen or global.hikariOpen需要开启,PlusUserServiceImpl @Service注解开启, DruidConf or HikariCPConf开启@Configuration
    //@Autowired
    private PlusUserService plusUserService;

    @Override
    public Boolean userAdd(User user) {
        return plusUserService.save(user);
    }

    @Override
    public User getById(Integer id) {
        return plusUserService.getById(id);
    }

    @Override
    public PageData<User> userSearch(QueryData<User> queryData) {
        PageData<User> pageData = new PageData<>(queryData.getPage(), queryData.getPageSize());
        IPage<User> iPage = new Page<>(pageData.getPage(), pageData.getPageSize());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        User user = queryData.getSearchData();
        if(user.getId() != null) {
            wrapper.eq(User::getId, user.getId());
        }
        wrapper.orderByDesc(User::getAddTime);
        plusUserService.page(iPage, wrapper);
        pageData.setDataList(iPage.getRecords());
        pageData.setTotalPages(iPage.getPages());
        pageData.setTotalRecords(iPage.getTotal());
        return pageData;
    }
}
